/* This file is part of HealthyFood.

   HealthyFood is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   HealthyFood is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with HealthyFood.  If not, see <http://www.gnu.org/licenses/>.

	Portions of this file are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 *	copyright (c) 2009 Vytautas Kasparavičius
 */

package com.healthyFood;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent; //import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ProductEdit extends Activity {
	private DbAdapter mDbHelper;
	private String title = "";
	private String contents = "";
	private Long barcode;
	private boolean addAction;
	
	private static final int BAD_FORMAT_DIALOG = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_edit);
		mDbHelper = new DbAdapter(this);
		mDbHelper.open();
		this.barcode = Long.parseLong("0");
		
		this.addAction = true; 
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			this.barcode = bundle.getLong("barcode");
			EditText barcodeEdit = (EditText) findViewById(R.id.BarcodeEdit);
			barcodeEdit.setText(barcode.toString());
			if (bundle.size() > 1) {
				this.addAction = false;
				Button addButton = (Button)findViewById(R.id.AddButton);
				addButton.setText(getString(R.string.update_ProductEdit));
				this.contents = (String)bundle.getString("contents");
				this.title = bundle.getString("title");
				EditText edit = (EditText) findViewById(R.id.ContentsEdit);
				edit.setText(this.contents);
				edit = (EditText)findViewById(R.id.TitleEdit);
				edit.setText(this.title);
			}
		}

		
		findViewById(R.id.CancelButton).setOnClickListener(lCancel);
		findViewById(R.id.AddButton).setOnClickListener(lAdd);
	}

	public final Button.OnClickListener lCancel = new Button.OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};

	public final Button.OnClickListener lAdd = new Button.OnClickListener() {
		public void onClick(View v) {
			Long barcode = Long.parseLong("0");
			String title = "";
			String contents = "";
			try {
				EditText edit = (EditText) findViewById(R.id.BarcodeEdit);
				barcode = Long.parseLong(edit.getText().toString());
				edit = (EditText) findViewById(R.id.TitleEdit);
				title = edit.getText().toString();
				edit = (EditText) findViewById(R.id.ContentsEdit);
				contents = edit.getText().toString();
			} catch (NumberFormatException e) {
			}
			if (barcode > 0 && title.length() > 3 && contents.length() > 5) {
				if (addAction) {
					mDbHelper.addProduct(barcode, title, contents);
					Toast.makeText(getApplicationContext(), R.string.productAdded_ProductEdit, Toast.LENGTH_SHORT).show();
				}
				else {
					mDbHelper.updateProduct(barcode, title, contents);
					Toast.makeText(getApplicationContext(), R.string.productUpdated_ProductEdit, Toast.LENGTH_SHORT).show();
				}
				
				finish();
			} else {
				showDialog(ProductEdit.BAD_FORMAT_DIALOG);
			}
		}
	};

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ProductEdit.BAD_FORMAT_DIALOG:
			return new AlertDialog.Builder(ProductEdit.this)
			.setTitle("Error").setMessage(getString(R.string.badFormat_ProductEdit))
			.setPositiveButton("OK",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
						}
					}).create();
		}
		return null;
	}
}
